package com.leyou.goodsDetail.service;

import com.leyou.goodsDetail.feign.Brandclient;
import com.leyou.goodsDetail.feign.Categoryclient;
import com.leyou.goodsDetail.feign.Goodsclient;
import com.leyou.goodsDetail.feign.Specclient;
import com.leyou.item.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author shkstart
 * @create 2020-05-04 14:14
 */

@Service
public class GoodsDetailService {

    @Autowired
    private Brandclient brandclient;
    @Autowired
    private Categoryclient categoryclient;
    @Autowired
    private Goodsclient goodsclient;
    @Autowired
    private Specclient specclient;



    public Map<String, Object> buildGoodsSetail(Long id) {

        Map<String, Object> map = new HashMap<>();

        Spu spuById = goodsclient.getSpuById(id);
        SpuDetail spuDetail = goodsclient.getSpuDetail(id);
        List<Sku> listSku = goodsclient.getListSku(id);

        List<Long> ids = Arrays.asList(spuById.getCid1(), spuById.getCid2(), spuById.getCid3());
        List<String> categoryNames = categoryclient.getCategoryNames(ids);
        List<Map<String, Object>> categorys = new ArrayList<>();
        for (int i = 0; i <categoryNames.size() ; i++) {
            Map<String, Object> categoryMap = new HashMap<>();
            categoryMap.put("id",ids.get(i));
            categoryMap.put("name",categoryNames.get(i));
            categorys.add(categoryMap);
        }

        Brand brandByBrandId = brandclient.getBrandByBrandId(spuById.getBrandId());

        List<SpecGroup> specGroups = specclient.groupAndParam(spuById.getCid3());

        List<SpecParam> specParam = specclient.getSpecParam(null, spuById.getCid3(), null, false);
        Map<Long, String> paramMap = new HashMap<>();
        specParam.forEach(param->{
            paramMap.put(param.getId(),param.getName());
        });

        // 封装spu
        map.put("spu", spuById);
        // 封装spuDetail
        map.put("spuDetail", spuDetail);
        // 封装sku集合
        map.put("skus", listSku);
        // 分类
        map.put("categories", categorys);
        // 品牌
        map.put("brand", brandByBrandId);
        // 规格参数组
        map.put("groups", specGroups);
        // 查询特殊规格参数
        map.put("params", paramMap);


        return map;

    }
}
